package com.FH.applyForm.officeServies.vacationtype;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/20.
 */
@Stateless
public class VacationTypeDAOImpl extends AccessDB implements VacationTypeDAO{

    /**
     * 查询休假申请一级菜单
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getVacationTypeInfo(String search ,String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from vacationtype  where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("officetool_id")) {
//                sql_w += " AND organization_id = "+json.getString("organization_id");
                sql_w += " AND officetool_id ='" + json.getString("officetool_id") + "'";
            }
            if (json.has("vacationtypename")) {
//                sql_w += " AND organization_id = "+json.getString("organization_id");
                sql_w += " AND vacationtypename ='" + json.getString("vacationtypename") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();

            List<VacationTypeShowDTO> result = new ArrayList<VacationTypeShowDTO>();

            if (!existName.equals("0")) {
                String sql = "SELECT id,vacation_type,vacationtypename FROM vacationtype WHERE 1=1 " + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    VacationTypeShowDTO vacationTypeShowDTO = new VacationTypeShowDTO();

                    vacationTypeShowDTO.setId(seresult.get(i)[1]);
                    vacationTypeShowDTO.setVacation_type(seresult.get(i)[2]);
                    vacationTypeShowDTO.setVacationtypename(seresult.get(i)[3]);


                    result.add(vacationTypeShowDTO);

                }
            }
            if (seresult.size() != 0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }

    /**
     * 添加休假类型
     * @param vacationTypePullDTO
     * @return
     */
    @Override
    public ResultDTO addVacationTypeInfo(VacationTypePullDTO vacationTypePullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String vacation_type=vacationTypePullDTO.getVacation_type();
        String sql="select * from vacationtype where vacation_type='"+vacation_type+"'";
        String  sum=getReportingToOne(sql);
        if(!sum.equals("")){

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("此休假类型信息已存在，请重新分配");
            return resultDTO;
        }
        String sql1="";
        sql1="insert into vacationtype (id,vacation_type)" +
                "values('"+uuid+"','"+vacationTypePullDTO.getVacation_type()+"')";

        resultls=addReportingTo(sql1);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

}
